<?php
    /*
    to send an XML document to the server in response to an AJAX request, 
    we need to send "header("Content-type: text/html") then the ill-formatted 
    XML (no <?xml version?> tag).
    */
    require_once('user.php');
    $action = $_REQUEST['action'];
    $name = $_REQUEST['username'];
    $pass = $_REQUEST['pass'];
    $proto = $_REQUEST['proto'];
    
    switch($action) {
        case "login": {
            $user = new User($name, $pass, $proto);
            $response = $user->login();
            if ($response) {
                $list = $user->friends_list();
                header("Content-type: text/xml");
                echo $list->to_xml();
            }
        }
            break;
        default:
            break;
    }
?>